package cn.diancan.service.impl;


import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Component;

import cn.diancan.dao.MyBaseDao;
import cn.diancan.exception.OutMsgException;
import cn.diancan.model.PurchaseSaleOrder;
import cn.diancan.model.ScoreRecord;
import cn.diancan.model.ShopCustomer;
import cn.diancan.model.types.CommonType;
import cn.diancan.model.types.ShopCustomerMemberType;
import cn.diancan.service.ShopCustomerService;
import cn.diancan.service.SysDataLogServie;
import cn.diancan.util.DateUtil;
import cn.diancan.util.ObjectUtils;
import cn.diancan.vo.ShopCustomerVo;
import cn.diancan.vo.other.Pager;

@Component("shopCustomerService")
public class ShopCustomerServiceImpl implements ShopCustomerService {

    @Resource(name = "MyBaseDao")
    private MyBaseDao<ShopCustomer> shopCustomerDao;
    
    @Resource(name = "sysDataLogServie")
    private SysDataLogServie sysDataLogServie;
    
    @Resource(name = "MyBaseDao")
    private MyBaseDao<ScoreRecord> scoreRecordDao; 
    @Resource(name="MyBaseDao")
    private MyBaseDao<PurchaseSaleOrder> purchaseOrderDao;
    
    
    @Override
    public ShopCustomerVo saveVipShopCustomer(String customerName,
	    String customerPhoneNum, Integer customerSex,
	    String customerBrithDay, String customerReMarks)throws OutMsgException,Exception {
	ShopCustomer shopCustomer = shopCustomerDao.get("FROM ShopCustomer WHERE customerPhoneNum=? ", new Object[]{customerPhoneNum});
	if(null!=shopCustomer){
	    throw new OutMsgException("账号已存在");
	}
	shopCustomer= new ShopCustomer();
	shopCustomer.init();
	shopCustomer.setCustomerBrithDay(customerBrithDay);
	shopCustomer.setCustomerName(customerName);
	shopCustomer.setCustomerPhoneNum(customerPhoneNum);
	shopCustomer.setCustomerReMarks(customerReMarks);
	shopCustomer.setCustomerSex(customerSex);
	shopCustomer.setMemberType(ShopCustomerMemberType.VIP.getType()); 
	shopCustomerDao.save(shopCustomer);
	
	return shopCustomerBean2Vo(shopCustomer);
    }
    
    
    @Override
    public ShopCustomerVo updateVipShopCustomer(Long shopCustomerId,String customerName,
	    Integer customerSex,
	    String customerBrithDay, String customerReMarks)
		    throws OutMsgException, Exception {
	ShopCustomer shopCustomer = shopCustomerDao.get("FROM ShopCustomer WHERE id=? ", new Object[]{shopCustomerId});
	if(null==shopCustomer){
	    throw new OutMsgException("账号不存在");
	}
	ShopCustomer sourceData = new ShopCustomer();
	BeanUtils.copyProperties(sourceData, shopCustomer);
	
	
	shopCustomer.setCustomerName(customerName); 
	shopCustomer.setCustomerSex(customerSex);
	shopCustomer.setCustomerBrithDay(customerBrithDay);
	shopCustomer.setCustomerReMarks(customerReMarks);
	shopCustomer.setModifyTime(DateUtil.currentDateTime());
	shopCustomerDao.update(shopCustomer);
	
	try {
	    sysDataLogServie.saveSysDataLog("shopCustomerId="+shopCustomer.getId(), sourceData, shopCustomer);
	} catch (Exception e) { 
	    e.printStackTrace();
	}
	
	
	return shopCustomerBean2Vo(shopCustomer);
    }
    
    
    
    @Override
    public ShopCustomerVo updateVipShopCustomer(Long shopCustomerId,String customerName,
	    String customerPhoneNum, Integer customerSex,
	    String customerBrithDay, String customerReMarks)
		    throws OutMsgException, Exception {
	ShopCustomer shopCustomer = shopCustomerDao.get("FROM ShopCustomer WHERE id=? ", new Object[]{shopCustomerId});
	if(null==shopCustomer){
	    throw new OutMsgException("账号不存在");
	}
	ShopCustomer sourceData = new ShopCustomer();
	BeanUtils.copyProperties(sourceData, shopCustomer);
	
	
	shopCustomer.setCustomerName(customerName);
	shopCustomer.setCustomerPhoneNum(customerPhoneNum);
	shopCustomer.setCustomerSex(customerSex);
	shopCustomer.setCustomerBrithDay(customerBrithDay);
	shopCustomer.setCustomerReMarks(customerReMarks);
	shopCustomer.setModifyTime(DateUtil.currentDateTime());
	shopCustomerDao.update(shopCustomer);
	
	sysDataLogServie.saveSysDataLog("shopCustomerId="+shopCustomer.getId(), sourceData, shopCustomer);
	
	
	return shopCustomerBean2Vo(shopCustomer);
    }



    private ShopCustomerVo shopCustomerBean2Vo(ShopCustomer shopCustomer) throws  Exception{
	if(null== shopCustomer){
	    return null;
	}
	ShopCustomerVo vo = new ShopCustomerVo();
	BeanUtils.copyProperties(vo, shopCustomer);
	vo.setShopCustomerId(shopCustomer.getId());
	return vo;
    }
    
    
    

    @Override
    public ShopCustomerVo findByPhoneNum(String customerPhoneNum)
	    throws OutMsgException, Exception {
	ShopCustomer shopCustomer = shopCustomerDao.get("FROM ShopCustomer WHERE customerPhoneNum=? ", new Object[]{customerPhoneNum});
	if(null!=shopCustomer){
	    return shopCustomerBean2Vo(shopCustomer);
	}
	return null;
    }

    @Override
    public boolean saveShopCustomer(ShopCustomer shopCustomer)   throws OutMsgException, Exception {
	if (null == shopCustomer) {
	    throw new OutMsgException("参数错误");
	}
	shopCustomerDao.save(shopCustomer);
	return true;
    }

    @Override
    public boolean deleteShopCustomerById(Long shopCustomerId)    throws OutMsgException, Exception {
	if (null == shopCustomerId || shopCustomerId <= 0l) {
	    throw new OutMsgException("参数错误");
	}
	ShopCustomer shopCustomer = findbyShopCustomerId(shopCustomerId);
	if (null == shopCustomer) {
	    throw new OutMsgException("参数错误");
	}
	shopCustomerDao.update(shopCustomer);
	return true;
    }

    @Override
    public boolean updateShopCustomer(ShopCustomer shopCustomer)    throws OutMsgException, Exception {
	if (null == shopCustomer) {
	    throw new OutMsgException("参数错误");
	}
	shopCustomerDao.update(shopCustomer);
	return true;
    }

    @Override
    public ShopCustomer findbyShopCustomerId(Long shopCustomerId)    throws OutMsgException, Exception {
	if (null == shopCustomerId || shopCustomerId <= 0l) {
	    throw new OutMsgException("参数错误");
	}
	return shopCustomerDao.get("FROM ShopCustomer WHERE id=? AND isDelete=?",
		new Object[] { shopCustomerId, 0 });
    }

    @Override
    public Pager findShopCustomerPager(Pager pager,String customerName,String customerPhoneNum )    throws OutMsgException, Exception {
	Integer pageNo = pager.getPageNumber();
	Integer row = pager.getPageSize();
	List<Object> param = new ArrayList<Object>();

	String hql = "FROM ShopCustomer WHERE isDelete=? ";
	param.add(CommonType.Delete.NO);
	
	if(ObjectUtils.isNotBlank(customerName)){
	    hql+=" AND  instr(customerName,?)>0 ";
	    param.add(customerName); 
	}
	
	if(ObjectUtils.isNotBlank(customerPhoneNum)){
	    hql+=" AND  instr(customerPhoneNum,?)>0 ";
	    param.add(customerPhoneNum); 
	}
	
	
	List<ShopCustomer> shopCustomers = shopCustomerDao.find(hql, param,	pageNo, row);
	pager.setList(shopCustomers);

	Long count = shopCustomerDao.count("SELECT COUNT(id) " + hql, param);
	 pager.setTotalCount(count.intValue());
	return pager;
    }

    @Override
    public List<ShopCustomer> findShopCustomerList(Integer pageNo, Integer row)	    throws OutMsgException, Exception {
	return shopCustomerDao.find("FROM ShopCustomer WHERE isDelete=? ",new Object[] { 0 }, pageNo, row);
    }


    @Override
    public ShopCustomerVo findByOutOrderNumber(
	    String purchaseSaleOutOrderNumber)
		    throws OutMsgException, Exception {
	ScoreRecord scoreRecord=scoreRecordDao.get("FROM ScoreRecord WHERE purchaseSaleOutOrderNumber=? ",new Object[]{purchaseSaleOutOrderNumber});
	if(null!=scoreRecord){
	    ShopCustomer shopCustomer=    shopCustomerDao.get("FROM ShopCustomer WHERE id=? ", new Object[]{scoreRecord.getShopCustomerId()});
	    return shopCustomerBean2Vo(shopCustomer);
	}
	
	return null;
    }
   
    
    
   

}
